package com.rn.gypsophila.dsalg.arrary;

import java.util.Arrays;

/**
 * 题目：
 * 给你一幅由 N × N 矩阵表示的图像，其中每个像素的大小为 4 字节。请你设计一种算法，将图像旋转 90 度。不占用额外内存空间能否做到？
 * 链接：<a href="https://leetcode.cn/leetbook/read/array-and-string/clpgd/">...</a>
 *
 * @author 然诺
 * @since 2023/2/26
 */
public class MatrixRotate {

    public static void rotate(int[][] matrix) {
        int len = matrix.length;
        int temp;
        // 对角线交换
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        // 镜像交换
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len / 2; j++) {
                temp = matrix[i][j];
                matrix[i][j] = matrix[i][len - j - 1];
                matrix[i][len - j - 1] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};
        System.out.println(Arrays.deepToString(matrix));
        rotate(matrix);
        System.out.println(Arrays.deepToString(matrix));
    }
}
